package com.recipeexchange.utils

import grails.test.GrailsUnitTestCase
import com.recipeexchange.domain.Recipe


class GenerateLocatorTests extends GrailsUnitTestCase {

    void setUp() {
        super.setUp()
        mockDomain Recipe
    }

    void testReplacesSpacesWithDashes() {
        String generatedLocator = GenerateLocator.generate("locator with spaces")

        assertEquals "locator-with-spaces", generatedLocator
    }

    void testRemovesSpeehMarks() {
        String generatedLocator = GenerateLocator.generate("locator's")
        assertEquals "locators", generatedLocator
    }

    void testRemovesDoubleSpeehMarks() {
        String generatedLocator = GenerateLocator.generate('locator"s')
        assertEquals "locators", generatedLocator
    }

    void testDoesNotGenerateDuplicateLocator() {
        String locator = 'duplicate'
        mockDomain Recipe, [new Recipe(locator: locator)]

        String generatedLocator = GenerateLocator.generate(locator)

        assertEquals "$locator-2", generatedLocator
    }

    void testDoesNotGenerateDuplicateLocatorWhenLocatorsIncludeDashes() {
        final def locator = 'locator'
        mockDomain Recipe, [new Recipe(locator: locator), new Recipe(locator: "$locator-2")]

        String generatedLocator = GenerateLocator.generate(locator)

        assertEquals "$locator-3", generatedLocator
    }

    void testLocatorsAreAllLowercased() {
        String lowercaseString = 'LowerCasePlease'
        String generatedLocator = GenerateLocator.generate(lowercaseString)
        assertEquals lowercaseString.toLowerCase(), generatedLocator
    }

    void testLocatorsWithNumbersAreAllLowercased() {
        String lowercaseString = 'LowerCasePlease-99'
        String generatedLocator = GenerateLocator.generate(lowercaseString)
        assertEquals lowercaseString.toLowerCase(), generatedLocator
    }

}
